package com.vctgo.accountSet.service.impl;

import java.util.*;

import com.vctgo.accountSet.mapper.ChinasMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.vctgo.accountSet.mapper.TbSetAccountsMapper;
import com.vctgo.accountSet.domain.TbSetAccounts;
import com.vctgo.accountSet.service.ITbSetAccountsService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vctgo.common.core.text.Convert;
import com.vctgo.common.core.utils.ServletUtils;
import com.vctgo.common.mybatisplus.constant.MybatisPageConstants;

import java.util.Date;
/**
 * 账套Service业务层处理
 *
 * @author lhc
 * @date 2024-12-16
 */
@Service
public class TbSetAccountsServiceImpl implements ITbSetAccountsService
{
    @Autowired
    private TbSetAccountsMapper tbSetAccountsMapper;



    /**
     * 查询账套
     *
     * @param id 账套主键
     * @return 账套
     */
    @Override
    public TbSetAccounts selectTbSetAccountsById(Long id)
    {

        return tbSetAccountsMapper.selectById(id);
    }

    /**
     * 查询账套列表-分页
     *
     * @param tbSetAccounts 账套
     * @return 账套
     */
    @Override
    public IPage<TbSetAccounts> selectTbSetAccountsPage(TbSetAccounts tbSetAccounts)
    {
        Page mpPage =new Page(Convert.toLong(ServletUtils.getParameterToInt(MybatisPageConstants.PAGE_NUM),1L)
                ,Convert.toLong(ServletUtils.getParameterToInt(MybatisPageConstants.PAGE_SIZE),10L));
        return tbSetAccountsMapper.selectTbSetAccountsList(mpPage,tbSetAccounts);
    }
    /**
     * 查询账套列表
     *
     * @param tbSetAccounts 账套
     * @return 账套
     */
    @Override
    public List<TbSetAccounts> selectTbSetAccountsList(TbSetAccounts tbSetAccounts)
    {
        return tbSetAccountsMapper.selectTbSetAccountsList(tbSetAccounts);
    }

    /**
     * 新增账套
     * 三级联动
     * @param tbSetAccounts 账套
     * @return 结果
     */

    @Override
    public int insertTbSetAccounts(TbSetAccounts tbSetAccounts) {
        // 复用传入的tbSetAccounts对象，避免额外创建对象（假设业务逻辑允许）
        List<String> selectedOptions = tbSetAccounts.getSelectedOptions();
        StringBuilder addressIdBuilder = new StringBuilder();
        // 使用StringBuilder更高效地拼接字符串（假设accountAddressId需要以特定分隔符拼接字符串形式存储）
        for (int i = 0; i < selectedOptions.size(); i++) {
            addressIdBuilder.append(selectedOptions.get(i));
            if (i < selectedOptions.size() - 1) {
                addressIdBuilder.append(","); // 逗号分隔存储数据
            }
        }
        tbSetAccounts.setAccountAddressId(addressIdBuilder.toString());

        // 赋值有效期七天以后
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, 7);
        tbSetAccounts.setAccountValidUntil(calendar.getTime());

        tbSetAccounts.setAccountCurrentAccountingYear(new Date());
        tbSetAccounts.setAccountCreatetime(new Date());

        return tbSetAccountsMapper.insert(tbSetAccounts);
    }

    /**
     * 修改账套
     *
     * @param tbSetAccounts 账套
     * @return 结果
     */
    @Override
    public int updateTbSetAccounts(TbSetAccounts tbSetAccounts)
    {
        return tbSetAccountsMapper.updateById(tbSetAccounts);
    }

    /**
     * 批量删除账套
     *
     * @param ids 需要删除的账套主键
     * @return 结果
     */
    @Override
    public int deleteTbSetAccountsByIds(Long[] ids)
    {
        return tbSetAccountsMapper.deleteTbSetAccountsByIds(ids);
    }

    /**
     * 删除账套信息
     *
     * @param id 账套主键
     * @return 结果
     */
    @Override
    public int deleteTbSetAccountsById(Long id)
    {
        return tbSetAccountsMapper.deleteById(id);
    }
}
